package com.leyou.item.service.impl;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private SpecGroupMapper groupMapper;

    @Autowired
    private SpecParamMapper paramMapper;

    /**
     * 根据分类id查询参数组
     * @param cid
     * @return
     */
    @Override
    public List<SpecGroup> queryGroupByCid(Long cid) {
        SpecGroup record = new SpecGroup();
        record.setCid(cid);
        return this.groupMapper.select(record);
    }

    /**
     * 根据分类id查询参数组和规格参数
     * @param cid
     * @return
     */
    @Override
    public List<SpecGroup> queryGroupsWithParam(Long cid) {
        List<SpecGroup> groups = this.queryGroupByCid(cid);
        groups.forEach(group -> {
            List<SpecParam> params = this.queryParams(group.getId(), null, null, null);
            group.setParams(params);
        });
        return groups;
    }

    /**
     * 根据条件查询规格参数
     * @param gid
     * @return
     */
    @Override
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setGeneric(generic);
        record.setSearching(searching);
        return this.paramMapper.select(record);
    }

    /**
     * 新增参数组
     * @param specGroup
     */
    @Override
    public void saveGroup(SpecGroup specGroup) {
        this.groupMapper.insertSelective(specGroup);
    }

    /**
     * 更新参数组
     * @param specGroup
     */
    @Override
    public void updateGroup(SpecGroup specGroup) {
        int count = this.groupMapper.updateByPrimaryKey(specGroup);
    }

    /**
     * 删除参数组
     * @param id
     * @return
     */
    @Override
    @Transactional
    public void deleteGroupById(Long id) {
        SpecGroup record = new SpecGroup();
        record.setId(id);
        SpecGroup specGroup = this.groupMapper.selectByPrimaryKey(record);
        // 先删除规格参数列表
        this.paramMapper.deleteByCid(specGroup.getCid());
        // 再删除参数组
        this.groupMapper.deleteByPrimaryKey(record);
    }

    /**
     * 新增规格参数
     * @param specParam
     */
    @Override
    public void saveParam(SpecParam specParam) {
        this.paramMapper.insertSelective(specParam);
    }

    /**
     * 更新规格参数
     * @param specParam
     */
    @Override
    public void updateParam(SpecParam specParam) {
        this.paramMapper.updateByPrimaryKey(specParam);
    }

    /**
     * 删除规格参数
     * @param id
     * @return
     */
    @Override
    public void deleteParamById(Long id) {
        SpecParam record = new SpecParam();
        record.setId(id);
        this.paramMapper.delete(record);
    }


}
